package com.lqe.study.Cookie;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Date;

@WebServlet("/CookieDemo01")
public class CookieDemo01 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");
        Cookie[] cookies = req.getCookies();


        for (int i = 0; i < cookies.length; i++) {
            if(cookies[i].getName().equals("LastLoginTime")) {
                String value = cookies[i].getValue();
                resp.getWriter().println("上次你登录的时间是：" + new Date(Long.valueOf(value)));
            }else{
                if(i<cookies.length-1){
                    continue;
                }else{
                    resp.getWriter().println("欢迎你的第一次到来！");
                }
            }
        }








        Cookie lastLoginTime = new Cookie("LastLoginTime", String.valueOf(System.currentTimeMillis()));
        resp.addCookie(lastLoginTime);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
